import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * ErrorReport.java
 *
 * Created on 7 de Fevereiro de 2005, 19:37
 */

/**
 *
 * @author Edison Puig Maldonado
 */

public class ErrorReport extends javax.swing.JPanel {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 4028323806467012562L;
	private SAPO sapo;
    
    /** Creates new form ErrorReport */
    public ErrorReport(SAPO sapo) {
        this.sapo = sapo;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel30 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtxtErrorReport = new javax.swing.JTextArea();
        jPanel29 = new javax.swing.JPanel();
        jbtnSalvaError = new javax.swing.JButton();
        jbtnEnviarEmail = new javax.swing.JButton();
        jbtnAjuda = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel30.setLayout(new java.awt.BorderLayout());

        jPanel30.setMinimumSize(new java.awt.Dimension(333, 100));
        jtxtErrorReport.setColumns(40);
        jtxtErrorReport.setEditable(false);
        jtxtErrorReport.setRows(20);
        jtxtErrorReport.setMinimumSize(new java.awt.Dimension(200, 100));
        jScrollPane1.setViewportView(jtxtErrorReport);

        jPanel30.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel29.setMinimumSize(new java.awt.Dimension(333, 200));
        jbtnSalvaError.setText("Salvar");
        jbtnSalvaError.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSalvaErrorActionPerformed(evt);
            }
        });

        jPanel29.add(jbtnSalvaError);

        jbtnEnviarEmail.setText("Enviar e-mail");
        jbtnEnviarEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnEnviarEmailActionPerformed(evt);
            }
        });

        jPanel29.add(jbtnEnviarEmail);

        jbtnAjuda.setText("Ajuda");
        jPanel29.add(jbtnAjuda);

        jPanel30.add(jPanel29, java.awt.BorderLayout.SOUTH);

        add(jPanel30, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents

    private void jbtnEnviarEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEnviarEmailActionPerformed
        LogError email = new LogError(sapo);
        email.sendMail(jtxtErrorReport);
    }//GEN-LAST:event_jbtnEnviarEmailActionPerformed

    private void jbtnSalvaErrorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSalvaErrorActionPerformed
        // Grava o arquivo de log de erros do SAPO:
        try{
          FileOutputStream file = new FileOutputStream("ErrorLog.txt");
          BufferedOutputStream buff = new BufferedOutputStream(file);
          DataOutputStream data = new DataOutputStream(buff);
          data.writeChars(jtxtErrorReport.getText());
          data.close();
        }//try  
        catch (IOException e){
            jtxtErrorReport.append(e.toString()+"\n");
            e.printStackTrace();
            sapo.error = e.getStackTrace();
            for(int k=0; k<sapo.error.length; k++)
                jtxtErrorReport.append(sapo.error[k].toString()+"\n");
            jtxtErrorReport.append("==========================================="+"\n");
            sapo.jdlgErrorReport.pack();
            sapo.jdlgErrorReport.setVisible(true);           
        }//catch
        sapo.jdlgErrorReport.setVisible(false);
    }//GEN-LAST:event_jbtnSalvaErrorActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel29;
    private javax.swing.JPanel jPanel30;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnAjuda;
    private javax.swing.JButton jbtnEnviarEmail;
    private javax.swing.JButton jbtnSalvaError;
    public javax.swing.JTextArea jtxtErrorReport;
    // End of variables declaration//GEN-END:variables
    
}
